from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship

from db.model.base import Base
from db.model.device import Device


class Slave(Base):
    __tablename__ = "slave"

    id = Column(Integer, primary_key=True, comment="id")
    device_id = Column(Integer, ForeignKey('device.id'))
    slave_id = Column(Integer, nullable=False)

    # 关联设备和寄存器配置
    device = relationship("Device", back_populates="slaves")
    register = relationship("Register", back_populates="slave")

    __table_args__ = (UniqueConstraint("device_id", "slave_id", name="uq_slave"), {"comment": "从站表"})
